import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:mybaseplus/base/tool/entitylog/MyEntityLog.dart';
import 'package:mybaseplus/base/tool/file/FFDirectory.dart';
import 'package:mybaseplus/base/tool/file/FFile.dart';

class MyEntityTestRun{

  static Future<void> createEntityCode({
    required Map map,
    required String targetDir,
    required String entityClassName,
    bool? forceFlush = false, //覆盖，慎用
  }) async {

    bool isDir = await FFDirectory.isExistDirectory(targetDir);
    if (isDir == false) {
      debugPrint("路径不存在,将为你自动创建 ${targetDir}");
    }

    if (!targetDir.endsWith(Platform.pathSeparator)) {
      targetDir = "${targetDir}${Platform.pathSeparator}";
    }

    String classCode = MyEntityLog.log(entityClassName, map);
    String filePath = "$targetDir$entityClassName.dart";

    if (forceFlush == true) {
      bool success = await FFile.writeContentByPath(filePath: filePath, content: classCode,mode: FileMode.write);
      if (success == true) {
        debugPrint("路径下类覆盖成功 ${filePath}");
      }  else {
        debugPrint("路径下类覆盖失败 ${filePath}");
      }
    } else {

      bool exist = await FFile.isExistFile(filePath);
      if (exist) {
        debugPrint("路径下类已经存在 ${filePath}");
      } else {
        bool success = await FFile.writeContentByPath(filePath: filePath, content: classCode,autoCreate: true);
        if (success == true) {
          debugPrint("路径下类生成成功 ${filePath}");
        } else {
          debugPrint("路径下类生成失败 ${filePath}");
        }

      }
    }
  }

  static Future<String> createEntityCodeStr({
    required Map map,
    required String targetDir,
    required String entityClassName,
    bool? forceFlush = false, //覆盖，慎用
  }) async {
    String result = "";
    bool isDir = await FFDirectory.isExistDirectory(targetDir);
    if (isDir == false) {
      result = "路径不存在,将为你自动创建 ${targetDir}";
    }

    if (!targetDir.endsWith(Platform.pathSeparator)) {
      targetDir = "${targetDir}${Platform.pathSeparator}";
    }

    String classCode = MyEntityLog.log(entityClassName, map);
    String filePath = "$targetDir$entityClassName.dart";

    if (forceFlush == true) {
      bool success = await FFile.writeContentByPath(filePath: filePath, content: classCode,mode: FileMode.write);
      if (success == true) {
        result = "路径下类覆盖成功 ${filePath}";
      }  else {
        result = "路径下类覆盖失败 ${filePath}";
      }
    } else {

      bool exist = await FFile.isExistFile(filePath);
      if (exist) {
        result = "路径下类已经存在 ${filePath}";
      } else {
        bool success = await FFile.writeContentByPath(filePath: filePath, content: classCode,autoCreate: true);
        if (success == true) {
          result = "路径下类生成成功 ${filePath}";
        } else {
          result = "路径下类生成失败 ${filePath}";
        }

      }
    }

    return result;
  }
}